package com.rose.redis._redis_config;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * <p>
 * Redis配置类
 * 项目上一般是显示Bean来声明连接池。
 * <p>
 * demo中按照application.yml来配置即可。
 * </p>
 *
 * @author: zhu.chen
 * @date: 2020/12/30
 * @version: v1.0.0
 */
//@Configuration
public class RedisConfiguration {

    /**
     * Redis-Standalone的配置参考：{@link RedisStandaloneConfiguration}
     * Redis-Sentinel配置参考：{@link org.springframework.data.redis.connection.RedisSentinelConfiguration}
     * Redis-Cluster配置参考：{@link org.springframework.data.redis.connection.RedisClusterConfiguration}
     */
    @Bean
    public RedisStandaloneConfiguration redisStandaloneConfiguration() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("");
        redisStandaloneConfiguration.setPort(Integer.parseInt(""));
        redisStandaloneConfiguration.setPassword("");
        redisStandaloneConfiguration.setDatabase(Integer.parseInt(""));
        return redisStandaloneConfiguration;
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration) {
        RedisConnectionFactory redisConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration);
        return redisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(RedisSerializer.json());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }

}
